//
//  ZZMusicMoreLightTool.m
//  ZZBlue
//
//  Created by stonesMonkey on 2017/10/3.
//  Copyright © 2017年 stonesMonkey. All rights reserved.
//

#import "ZZMusicMoreLightTool.h"

#define kMusicMoreLightToolKey @"MusicMoreLightToolKey"
#define kZZMusicMoreLightToolPath @"ZZMusicMoreLightToolPath.plist"

@implementation ZZMusicMoreLightTool

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    
    static id listGroupModel = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        listGroupModel = [super allocWithZone:zone];
        
    });
    
    return listGroupModel;
}



- (void)save {
    
    //准备路径:
    NSString *path = NSHomeDirectory();
    NSLog(@"%@",path);
    
    path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",kZZMusicMoreLightToolPath]];
    //1:准备存储数据的对象
    NSMutableData *data = [NSMutableData data];
    //2:创建归档对象
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    //3:开始归档
    [archiver encodeObject:self.lightGroup forKey:kMusicMoreLightToolKey];
    //4:完成归档
    [archiver finishEncoding];
    //5:写入文件当中
    BOOL result = [data writeToFile:path atomically:YES];
    if (result) {
        
        // 写入成功
    }
}

- (void)deleteModel:(ZZLightViewCellModel *)model {
    
    [self.lightGroup.models removeObject:model];
    
    [self save];
}

- (void)addModel:(ZZLightViewCellModel *)model {
    
    [self.lightGroup.models addObject:model];
    
    [self save];
}

- (ZZLightViewGroupModel *)lightGroup {
    
    if (!_lightGroup) {
        
        // 从数据中拿
        NSString *path = NSHomeDirectory();
        path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",kZZMusicMoreLightToolPath]];
        NSData *myData = [NSData dataWithContentsOfFile:path];
        //创建反归档对象
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:myData];
        //反归档
        ZZLightViewGroupModel* listGroupModel = [unarchiver decodeObjectForKey:kMusicMoreLightToolKey];
        //完成反归档
        [unarchiver finishDecoding];
        
        if (listGroupModel == nil) {
            
            listGroupModel = [ZZLightViewGroupModel new];
            
            ZZLightViewCellModel *model1 = [ZZLightViewCellModel new];
            model1.title = NSLocalizedString(@"MUSIC_Cell_TITLE1", nil);
            model1.model = LightModelTiaoBian;
            model1.colorZuHeShu = 5;
            for (NSInteger index = 0; index< 5; index++) {
                
                [model1.colorsGroup addObject: [NSMutableArray arrayWithArray:@[[ZZColorRGBA colorRGBAWithR:255.0 G:0 B:0 A:1]]]];
            }
            model1.imageSize = CGSizeMake(130, 130);
            [model1 reloadImage];
            
            ZZLightViewCellModel *model2 = [ZZLightViewCellModel new];
            model2.title = NSLocalizedString(@"MUSIC_Cell_TITLE2", nil);;
            model2.model = LightModelTiaoBian;
            model2.imageSize = CGSizeMake(130, 130);
            
            
            model2.colorZuHeShu = 5;
            for (NSInteger index = 0; index< 5; index++) {
                
                [model2.colorsGroup addObject: [NSMutableArray arrayWithArray:@[[ZZColorRGBA colorRGBAWithR:255.0 G:0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:0 G:255.0 B:0 A:1]]]];
            }
            [model2 reloadImage];
            
            
            ZZLightViewCellModel *model3 = [ZZLightViewCellModel new];
            model3.title = NSLocalizedString(@"MUSIC_Cell_TITLE3", nil);;
            model3.model = LightModelTiaoBian;
            
            model3.colorZuHeShu = 5;
            for (NSInteger index = 0; index< 5; index++) {
                
                [model3.colorsGroup addObject: [NSMutableArray arrayWithArray:@[[ZZColorRGBA colorRGBAWithR:255.0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:255.0 G:0 B:0 A:1]]]];
            }
            model3.tuoWeiLength = 0.6;
            model3.imageSize = CGSizeMake(130, 130);
            [model3 reloadImage];
            
            ZZLightViewCellModel *model4 = [ZZLightViewCellModel new];
            model4.title = NSLocalizedString(@"MUSIC_Cell_TITLE4", nil);;
            model4.model = LightModelTiaoBian;
            model4.colorZuHeShu =5;
            for (NSInteger index = 0; index< 5; index++) {
                
                [model4.colorsGroup addObject:  [NSMutableArray arrayWithArray:@[[ZZColorRGBA colorRGBAWithR:255.0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:255.0 G:0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:0 G:0 B:255.0 A:1]]]];
            }
            
            model4.tuoWeiLength = 0.2;
            model4.imageSize = CGSizeMake(130, 130);
            [model4 reloadImage];
            
            [listGroupModel.models addObject:model1];
            [listGroupModel.models addObject:model2];
            [listGroupModel.models addObject:model3];
            [listGroupModel.models addObject:model4];
            
            {
                ZZLightViewCellModel *model1 = [ZZLightViewCellModel new];
                model1.title = NSLocalizedString(@"MUSIC_Cell_TITLE5", nil);;
                model1.model = LightModelTiaoBian;
                model1.colorZuHeShu =5;
                for (NSInteger index = 0; index< 5; index++) {
                    
                    [model1.colorsGroup addObject: [NSMutableArray arrayWithArray:@[[ZZColorRGBA colorRGBAWithR:255.0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:255.0 G:0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:255.0 G:0 B:0 A:1]]]];
                }
                model1.imageSize = CGSizeMake(130, 130);
                [model1 reloadImage];
                
                ZZLightViewCellModel *model2 = [ZZLightViewCellModel new];
                model2.title = NSLocalizedString(@"MUSIC_Cell_TITLE6", nil);;
                model2.isSelect = YES;
                model2.model = LightModelTiaoBian;
                
                model2.colorZuHeShu = 5;
                for (NSInteger index = 0; index< 5; index++) {
                    
                    [model2.colorsGroup addObject: [NSMutableArray arrayWithArray:@[[ZZColorRGBA colorRGBAWithR:255.0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:255.0 G:0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:255.0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:255.0 G:0 B:0 A:1]]]];
                }
                model2.imageSize = CGSizeMake(130, 130);
                [model2 reloadImage];
                
                [listGroupModel.models addObject:model1];
                [listGroupModel.models addObject:model2];
            }
            
            
        }
        
        _lightGroup = listGroupModel;
        [self save];
    }
    
    return _lightGroup;
}

@end
